package cn.tedu.cgb2202.sk.common.web;

import lombok.Data;

import java.io.Serializable;

/**
 * 2022.6.14
 * 胡然
 * 该类负责将数据转化为Json格式
 */
@Data
public class JsonResult<T> implements Serializable {

    private Integer state;
    private String message;
    private T data;

    private JsonResult(){}

    public static JsonResult<Void> ok(){
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setState(State.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult<Void> fail(State state, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.setState(state.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
